﻿Public Class F11_Loai_Chi
    Dim ds As List(Of LoaiChi)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        If (MessageBox.Show("Bạn muốn thoát chương trình?", "Thông báo", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
        End If

    End Sub
    Sub HienThi()
        Dim stt As Integer = 1
        ds = XL_LoaiChi.LayDS()
        lvloaichi.Items.Clear()
        For Each p In ds
            Dim li As ListViewItem = New ListViewItem()
            li.Tag = p
            li.Text = stt
            li.SubItems.Add(p.Loai)
            lvloaichi.Items.Add(li)
            stt = stt + 1
        Next
    End Sub

    Private Sub btnthem_Click(sender As Object, e As EventArgs) Handles btnthem.Click
        If (tbxloaichi.Text = "") Then
            MessageBox.Show("Chưa thêm tên loại chi!", "Thông Báo")
            Return
        End If
        Dim lc As LoaiChi = New LoaiChi
        lc.Loai = tbxloaichi.Text
        XL_LoaiChi.Them(lc)
        HienThi()
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        If lvloaichi.SelectedItems.Count > 0 Then
            Try
                Dim lc As LoaiChi = lvloaichi.SelectedItems(0).Tag
                lc.Loai = tbxloaichi.Text()
                XL_LoaiChi.CapNhat(lc)
                HienThi()
            Catch ex As Exception
                Exit Sub
            End Try
        Else
            MessageBox.Show("Chọn loại chi cần cập nhật!", "Thông báo")
        End If
    End Sub

    Private Sub F11_Loai_Chi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HienThi()
    End Sub

    Private Sub btnxoa_Click(sender As Object, e As EventArgs) Handles btnxoa.Click
        If lvloaichi.SelectedItems.Count > 0 Then
            If (MessageBox.Show("Bạn muốn xóa dữ liệu này?", "Cảnh báo", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes) Then
                Dim lc As LoaiChi = lvloaichi.SelectedItems(0).Tag
                XL_LoaiChi.Xoa(lc)
                HienThi()
            End If

        Else
            MessageBox.Show("Chọn phiếu cần xóa!", "Thông báo")
        End If
    End Sub

    Private Sub lvloaichi_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvloaichi.SelectedIndexChanged
        If (lvloaichi.SelectedItems.Count > 0) Then
            Dim lc As LoaiChi = lvloaichi.SelectedItems(0).Tag
            tbxloaichi.Text = lc.Loai
        End If
    End Sub
End Class